<?php
namespace org\crawlgenie\modules;
use org\crawlgenie\application\environment\Request;
use org\crawlgenie\application\base\Factory;
use org\crawlgenie\application\document\Document;
use org\crawlgenie\application\filter\Filter;
use org\crawlgenie\models as Models;
use \Logger;
use \CGException;

/**
 * Description of DefaultCrawlerController
 *
 * @author Jinson
 */

abstract class DefaultCrawlerController {
    private $_validActions = null;
    private $_currentAction = null;

    private $_requestData = null;

    protected $_document = null;
    
    protected $_logger = null;

    
    public function __construct() {
//        Logger::getLogger(__CLASS__)->info(__METHOD__);
        $this->_init();
    }

    
    /**
     * Initialize method to create Document container object for any required models
     * as well as set the allowed page/service actions
     */
    protected function _init() {
        if($this->_document === null) {
            $this->_document = Factory::getDocument();
            $msg = 'Response Document object created';
            Logger::getLogger(__CLASS__)->info(__METHOD__ . ': ' . $msg);
        }

        $msg = 'Valid actions registered: ' . print_r($this->_validActions, true);
        Logger::getLogger(__CLASS__)->info(__METHOD__ . ': ' . $msg);
    }


    /**
     * Base execute method for CrawlerController
     * @exception CGException  Thrown when we get an invalid crawler request
     */
    public function execute() {
        $this->_currentAction = (\in_array(Request::getParam('crawler_action'), $this->_validActions)) ? Request::getParam('crawler_action') : null;

        if($this->_currentAction == null) {
            Logger::getLogger(__METHOD__)->error('Invalid crawler action: '. Request::getParam('crawler_action'));
            new CGException('Invalid crawler action request: ' . Request::getParam('crawler_action'));

        } else {
            Logger::getLogger(__METHOD__)->info('Crawler: ' . Request::getParam('mod') . ', and crawler action: ' . Request::getParam('crawler_action'));
        }
    }


    protected function _setValidActions(array $actionsArray) {
        $this->_validActions = $actionsArray;
    }


    protected function getCurrentAction() {
        return $this->_currentAction;
    }


    protected function setRequestData($data) {
        $this->_requestData = (is_array($data)) ? print_r($data,true) : $data;
        file_put_contents('received.txt', $this->_requestData);
    }

    protected function getRequestData() {
        return $this->_requestData;
    }
}
?>
